package com.lcq.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.lcq.gulimall.product.entity.ProductAttrValueEntity;
import com.lcq.gulimall.product.vo.AttrRespVo;
import com.lcq.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lcq.gulimall.product.service.AttrService;
import com.lcq.common.utils.PageUtils;
import com.lcq.common.utils.R;



/**
 * 商品属性
 *
 * @author lichuanqi
 * @email lichuanqi0086@163.com
 * @date 2021-02-25 19:28:13
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }
    /**
     * 获取spu规格
     * /product/attr/base/listforspu/{spuId}
     * */
    @RequestMapping("/base/listforspu/{spuId}")
    //@RequiresPermissions("product:attr:list")
    public R listForSpu(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> list = attrService.listForSpu(spuId);

        return R.ok().put("data", list);
    }
    /**
     * 修改spu规格
     * /product/attr/update/{spuId}
     * */
    @RequestMapping("/update/{spuId}")
    //@RequiresPermissions("product:attr:list")
    public R updateSpuAttr(@PathVariable("spuId") Long spuId, @RequestBody List<ProductAttrValueEntity> spuAttrs){
        attrService.updateSpuAttr(spuId,spuAttrs);
        return R.ok();
    }
    /**
     * 获取分类规格参数
     */
    @RequestMapping("/{type}/list/{catelogId}")
    //@RequiresPermissions("product:attr:list")
    public R baseList(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId,@PathVariable("type") String type){
        //PageUtils page = attrService.queryPage(params);
        PageUtils page = attrService.queryBasePage(params,catelogId,type);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		AttrRespVo attr = attrService.getDetail(attrId);
        return R.ok().put("attr", attr);
    }

    /**
     * 信息
     */
    @RequestMapping("/name/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R name(@PathVariable("attrId") Long attrId){
        String name = attrService.getNameById(attrId);
        return R.ok().put("name", name);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){
		attrService.save(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr){
		//attrService.updateById(attr);
        attrService.updateAttr(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

    @RequestMapping("/hello")
    //@RequiresPermissions("product:attr:delete")
    public String hello(){

        return "hello";
    }

}
